package com.spsy.order.init;

import com.alibaba.fastjson2.JSON;
import com.spsy.order.service.ICart2Service;
import com.spsy.order.service.ICartService;
import com.spsy.order.vo.CarItemRedisVo;
import com.spsy.order.vo.Cart2AndCartItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liuzhiping
 * @date: 2025/5/15 14:56
 **/
//@Component
@Slf4j
public class CartData {

    @Autowired
    private ICartService iCartService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ICart2Service iCart2Service;

    //初始化把数据库中的购物车数据放到redis缓存中
    //@PostConstruct
    public void init(){

        //优化代码，原来是一张表；现在要换成2张表，关联的


        //List<Cart> list =  iCartService.selectCartList(null);
        //OOM内存异常
        List<Cart2AndCartItemVo> list = iCart2Service.rofgihjrek3wplenrfjglkrewkmkf();

        log.info("===================================");
        log.info("--------->初始化 list:{}",JSON.toJSONString(list));
        log.info("===================================");



        //userId   map<"productId",quantity>
        Map<String,Map<String,Integer>> map = new HashMap<>();
        /*Map<String,String> map = new HashMap<>();

        for(Cart2AndCartItemVo vo : list){
            String userId = String.valueOf(vo.getUserId().longValue());
            String productId = String.valueOf(vo.getProdcutId().longValue());
            int quantity = vo.getQuantity().intValue();

            Object obj = this.redisTemplate.opsForHash().get(userId,productId);
            if(null == obj){
                redisTemplate.opsForHash().put(userId,productId,quantity);
            }else{
                redisTemplate.opsForHash().put(userId,productId,quantity+(int)obj);
            }
        }*/




        //定义一个变量来存储值用-零时变量
        //<userId,List<CarItemRedisVo>>
        //Map<String,List<CarItemRedisVo>> map = new HashMap<String, List<CarItemRedisVo>>();
        //购物车--->主键userId，值是物品集合放到redis中
        /*for(Cart2AndCartItemVo vo : list){
            String userId = String.valueOf(vo.getUserId().intValue());
            Long prodcutId = vo.getProdcutId();
            Long quantity = vo.getQuantity();

            List<CarItemRedisVo> value = map.get(userId);
            if(null == value){
                value = new ArrayList<>();

                CarItemRedisVo cartItemRedisVo =  new CarItemRedisVo();
                cartItemRedisVo.setQuantity(quantity);
                cartItemRedisVo.setProdcutId(prodcutId);

                value.add(cartItemRedisVo);

                map.put(userId,value);
            }else{
                boolean boo = false;
                for(CarItemRedisVo redisVo : value){
                    Long prodcutIdRedis = redisVo.getProdcutId();
                    Long quantityRedis = redisVo.getQuantity();
                    if(prodcutIdRedis.longValue() == prodcutId.longValue()){
                        quantityRedis += quantity;
                        redisVo.setQuantity(quantityRedis);
                        boo = true;
                        break;
                    }
                }
                if(!boo){
                    CarItemRedisVo cartItemRedisVo =  new CarItemRedisVo();
                    BeanUtils.copyProperties(vo,cartItemRedisVo);
                    value.add(cartItemRedisVo);
                }
            }
        }*/

        log.info("===================================");
        log.info("--------->初始化 map:{}",JSON.toJSONString(map));
        log.info("===================================");


        //初始化数据到redis中
        for(String userId :map.keySet()){
            this.redisTemplate.opsForValue().set(userId,JSON.toJSONString(map.get(userId)));
        }



        //把map的数据放到redis中
        //for(Long key : map.keySet()){
            //this.redisTemplate.opsForValue().set(String.valueOf(key.intValue()),map.get(key));
        //}



        /*or(Cart cart : list){
            String key = OrderRedisKey.ORDER_CART_USERID +cart.getUserId();
            this.redisTemplate.opsForValue().set(key, JSON.toJSONString(cart));
        }
*/
    }
}
